<template>
	<view class="content-baisc-layout">
		<view class="page-title u-text-center">
			为了更好的服务您，请补充以下信息！
		</view>
		<view class="content-area">
			<u-form ref="uForm">
				<u-form-item leftIcon="account-fill" :border-bottom="false">
					<u-input v-model="form.name" placeholder="请输入您的真实姓名" class="u-padding-left-24" />
				</u-form-item>
				<u-form-item leftIcon="lock-fill" :border-bottom="false">
					<u-input v-model="form.id_code" placeholder="请输入您的身份证号码" class="u-padding-left-24" />
				</u-form-item>
				<u-form-item leftIcon="weixin-fill" :border-bottom="false">
					<u-input v-model="form.wechat_code" placeholder="请输入您的微信号" class="u-padding-left-24" />
				</u-form-item>
				<u-form-item leftIcon="calendar-fill" :border-bottom="false">
					<u-input v-model="form.years_working" placeholder="请输入从业年限(年)" class="u-padding-left-24" />
				</u-form-item>
				<u-form-item leftIcon="account" :border-bottom="false">
					<u-input v-model="businessText" placeholder="选择从事事业" type="select" class="u-padding-left-24" @click="showBusiness = true" />
				</u-form-item>
				<u-form-item leftIcon="map-fill" :border-bottom="false">
					<u-input v-model="address" placeholder="选择从事地域" type="select" class="u-padding-left-24"
						@click="showRegion = true" />
				</u-form-item>
			</u-form>

			<view class="u-flex-col u-row-center">
				<view class="save-data-button u-text-center" @click="uploadAuthenticationInformation">
					确定
				</view>
				
				<view class="u-text-center u-padding-top-24 u-light-color" @click="skip">
					跳过
				</view>
				<u-select v-model="showBusiness" @confirm="businessChange" :list="businessLists"></u-select>
				<u-select v-model="showRegion" mode="mutil-column-auto" @confirm="regionChange" :list="lists"></u-select>
			</view>
		</view>
		<official-accounts v-if="focusCompletionStatus" @focusCompletion="focusCompletionFun"></official-accounts>
	</view>
</template>

<script>
	import area from '@/utils/area';
	import Cache from "@/utils/cache";
	import {BACK_URL} from '@/config/cachekey';
	import { isWeixinClient } from '@/utils/tools';
	import businessLists from './businessLists.json';
	import {authentication, getUserAuthentication} from '@/api/user'
	export default {
		data() {
			return {
				form: {
					name: '',
					id_code: '',
					wechat_code: '',
					years_working: '',
					business: '',
					province_id: '',
					city_id: ''
				},
				address: '',
				businessText:'',
				showRegion: false,
				lists: [],
				showBusiness: false,
				businessLists: businessLists,
				focusCompletionStatus:false
			};
		},
		onLoad() {
			this.getUserAuthenticationFun()
			// #ifdef H5
				if(isWeixinClient())this.focusCompletionStatus = true;
			// #endif
			this.$nextTick(() => {
				area.forEach((v, k) => {
					let d = v;
					v['children'].forEach((item, index) => {
						delete item['children'];
						d['children'][index] = item
					})
					this.lists[k] = d
				})
			})
		},
		methods: {
			async getUserAuthenticationFun(){
				let {
					code,
					data
				} = await getUserAuthentication();
				
				if(data == 1){
					this.$u.toast('您已完成信息认证');
					this.skip()
				}
			},
			regionChange(region) {
				this.form.province_id = region[0].value;
				this.form.city_id = region[1].value;
				this.address = region[0].label + " " + region[1].label
			},
			businessChange(e){
				this.form.business = e[0].value;
				this.businessText = e[0].label
			},
			async uploadAuthenticationInformation(){
				let {
					msg,
					code,
					data
				} = await authentication(this.form);
				
				if(code == 1){
					this.skip()
				}else{
					return this.$u.tosat(msg)
				}
			},
			skip(){
				let url = Cache.get(BACK_URL);
				
				if(uni.$u.test.isEmpty(url)){
					uni.redirectTo({
						url:'/pages/index/index'
					})
				}else{
					uni.redirectTo({
						url
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	@import './authentication.scss'
</style>